package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.dto.AllHisDiseaseDto;
import com.woniu.dto.HisDiseaseDto;
import com.woniu.dto.PageDTO;
import com.woniu.es.moudle.Disease;
import com.woniu.es.repository.DiseaseRepo;
import com.woniu.moudle.HisDisease;
import com.woniu.mapper.HisDiseaseMapper;
import com.woniu.param.AddHisDiseaseParam;
import com.woniu.param.ListHisDiseaseParam;
import com.woniu.param.ModifyDiseaseParam;
import com.woniu.service.HisDiseaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author w
 * @since 2021-11-11
 */
@Service
@Slf4j
public class HisDiseaseServiceImpl extends ServiceImpl<HisDiseaseMapper, HisDisease> implements HisDiseaseService {
    @Resource
    private HisDiseaseMapper hisDiseaseMapper;
    @Resource
    private DiseaseRepo diseaseRepo;
    @Override
    public HisDiseaseDto newMedicalRecord(AddHisDiseaseParam addHisDiseaseParam) {
        HisDisease hisDisease = new HisDisease();
        BeanUtils.copyProperties(addHisDiseaseParam,hisDisease);
        hisDiseaseMapper.insert(hisDisease);
        if(hisDisease.getDiseaseId()!=null){
            Disease disease = new Disease();
            BeanUtils.copyProperties(hisDisease,disease);
            diseaseRepo.save(disease);
        }

        HisDiseaseDto hisDiseaseDto = new HisDiseaseDto();
        BeanUtils.copyProperties(hisDisease,hisDiseaseDto);
        return hisDiseaseDto;
    }

    @Override
    public Boolean deleteDisease(ArrayList<Integer> ids) {
        int i = hisDiseaseMapper.deleteBatchIds(ids);
        if(i>0){
            for (Integer id : ids) {
                diseaseRepo.deleteById(id);
            }
        }
        return i>0?true:false;
    }

    @Override
    public HisDiseaseDto modifyDisease(ModifyDiseaseParam modifyDiseaseParam) {
        HisDisease hisDisease = new HisDisease();
        BeanUtils.copyProperties(modifyDiseaseParam,hisDisease);
        int i = hisDiseaseMapper.updateById(hisDisease);
//        if(i>0){
//            Optional<Disease> byId = diseaseRepo.findById(modifyDiseaseParam.getDiseaseId());
//            Disease x = byId.get();
//            if(hisDisease.getDiseaseName()!=null){
//                x.setDiseaseName(hisDisease.getDiseaseName());
//            }
//            if(hisDisease.getResultItemContent()!=null){
//                x.setResultItemContent(hisDisease.getResultItemContent());
//            }
//            if(hisDisease.getResultItemContentLabel()!=null){
//                x.setResultItemContentLabel(hisDisease.getResultItemContentLabel());
//            }
//            if(hisDisease.getResultItemContentLabel1()!=null){
//                x.setResultItemContentLabel1(hisDisease.getResultItemContentLabel1());
//            }
//            if(hisDisease.getResultItemContentLabel2()!=null){
//                x.setResultItemContentLabel2(hisDisease.getResultItemContentLabel2());
//            }
//            if(hisDisease.getResultItemContentLabel3()!=null){
//                x.setResultItemContentLabel3(hisDisease.getResultItemContentLabel3());
//            }
//            if(hisDisease.getResultItemContentLabel4()!=null){
//                x.setResultItemContentLabel4(hisDisease.getResultItemContentLabel4());
//            }
//            if(hisDisease.getView()!=null){
//                x.setView(hisDisease.getView());
//            }
//            x.setRawUpdateTime(new LocalDateTime(new Date()));
//            System.out.println(x);
//        }
        HisDiseaseDto hisDiseaseDto = new HisDiseaseDto();
        BeanUtils.copyProperties(hisDisease,hisDiseaseDto);

        return hisDiseaseDto;
    }

    @Override
    public AllHisDiseaseDto queryDisease(ListHisDiseaseParam listHisDiseaseParam) {
//        QueryWrapper<HisDisease> hisDiseaseQueryWrapper = new QueryWrapper<>();
//        if(listHisDiseaseParam!=null){
//            if(listHisDiseaseParam.getDiseaseId()!=null){
//                hisDiseaseQueryWrapper.eq("disease_id",listHisDiseaseParam.getDiseaseId());
//            }
//            if(listHisDiseaseParam.getDiseaseName()!=null){
//                hisDiseaseQueryWrapper.like("disease_name",listHisDiseaseParam.getDiseaseName());
//            }
//            if(listHisDiseaseParam.getResultItemContent()!=null){
//                hisDiseaseQueryWrapper.eq("result_item_content",listHisDiseaseParam.getResultItemContent());
//            }
//            if(listHisDiseaseParam.getResultItemContentLabel()!=null){
//                hisDiseaseQueryWrapper.like("result_item_content_label",listHisDiseaseParam.getResultItemContentLabel());
//            }
//            if(listHisDiseaseParam.getResultItemContentLabel1()!=null){
//                hisDiseaseQueryWrapper.like("result_item_content_label1",listHisDiseaseParam.getResultItemContentLabel1());
//            }
//            if(listHisDiseaseParam.getResultItemContentLabel2()!=null){
//                hisDiseaseQueryWrapper.like("result_item_content_label2",listHisDiseaseParam.getResultItemContentLabel2());
//            }
//            if(listHisDiseaseParam.getResultItemContentLabel3()!=null){
//                hisDiseaseQueryWrapper.like("result_item_content_label3",listHisDiseaseParam.getResultItemContentLabel3());
//            }
//            if(listHisDiseaseParam.getResultItemContentLabel4()!=null){
//                hisDiseaseQueryWrapper.like("result_item_content_label4",listHisDiseaseParam.getResultItemContentLabel4());
//            }
//            if(listHisDiseaseParam.getView()!=null){
//                hisDiseaseQueryWrapper.eq("view",listHisDiseaseParam.getView());
//            }
//        }
//        Page<HisDisease> tPage = page(new Page<HisDisease>(listHisDiseaseParam.getPageParam().getPage(), listHisDiseaseParam.getPageParam().getPageSize()), hisDiseaseQueryWrapper);
//        System.out.println(tPage);
//        return PageDTO.bulid(tPage,HisDiseaseDto.class);
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        if(listHisDiseaseParam.getResultItemContentLabel()!=null){
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("resultItemContentLabel4",listHisDiseaseParam.getResultItemContentLabel()))
                    .withQuery(QueryBuilders.matchQuery("resultItemContentLabel3",listHisDiseaseParam.getResultItemContentLabel()))
                    .withQuery(QueryBuilders.matchQuery("resultItemContentLabel2",listHisDiseaseParam.getResultItemContentLabel()))
                    .withQuery(QueryBuilders.matchQuery("resultItemContentLabel1",listHisDiseaseParam.getResultItemContentLabel()))
                    .withQuery(QueryBuilders.matchQuery("resultItemContentLabel",listHisDiseaseParam.getResultItemContentLabel()));
        }
        if(listHisDiseaseParam.getResultItemContent()!=null){
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("resultItemContent",listHisDiseaseParam.getResultItemContent()));
        }
        if(listHisDiseaseParam.getDiseaseName()!=null){
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("diseaseName",listHisDiseaseParam.getDiseaseName()));
        }
        if(listHisDiseaseParam.getView()!=null){
            System.out.println(1);
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("view",listHisDiseaseParam.getView()));
        }
        if(listHisDiseaseParam.getDiseaseId()!=null){
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("_id",listHisDiseaseParam.getDiseaseId()));
        }
//        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("resultItemContentLabel4",listHisDiseaseParam.getResultItemContentLabel()))
//                .withQuery(QueryBuilders.matchQuery("resultItemContentLabel3",listHisDiseaseParam.getResultItemContentLabel()))
//                .withQuery(QueryBuilders.matchQuery("resultItemContentLabel2",listHisDiseaseParam.getResultItemContentLabel()))
//                .withQuery(QueryBuilders.matchQuery("resultItemContentLabel1",listHisDiseaseParam.getResultItemContentLabel()))
//                .withQuery(QueryBuilders.matchQuery("resultItemContentLabel",listHisDiseaseParam.getResultItemContentLabel()))
//                .withQuery(QueryBuilders.matchQuery("resultItemContent",listHisDiseaseParam.getResultItemContent()))
//                .withQuery(QueryBuilders.matchQuery("diseaseName",listHisDiseaseParam.getDiseaseName()))
//                .withQuery(QueryBuilders.matchQuery("_id",listHisDiseaseParam.getDiseaseId()))
//                .withQuery(QueryBuilders.matchQuery("\t\n" +
//                        "view",listHisDiseaseParam.getView()));
//        log.info("当前页:{}",listHisDiseaseParam.getPageParam().getPage().intValue());
        if(listHisDiseaseParam.getPageParam()!=null){
            nativeSearchQueryBuilder.withPageable(PageRequest.of(listHisDiseaseParam.getPageParam().getPage().intValue()-1,listHisDiseaseParam.getPageParam().getPageSize().intValue()));
        }

        Page<Disease> search = diseaseRepo.search(nativeSearchQueryBuilder.build());
        //查询数据
        List<Disease> content = search.getContent();


        List<HisDiseaseDto> hisDiseaseDtos = CopyBeanUtil.copyBean(content, HisDiseaseDto.class);

        AllHisDiseaseDto allHisDiseaseDto = new AllHisDiseaseDto();
        if(listHisDiseaseParam.getPageParam()!=null){
            //总条数
            long totalElements = search.getTotalElements();
            allHisDiseaseDto.setPageSize(totalElements);
            allHisDiseaseDto.setIndexPage(listHisDiseaseParam.getPageParam().getPage());
        }

        allHisDiseaseDto.setList(hisDiseaseDtos);
        return allHisDiseaseDto;
    }
}
